<?php
    class Default_Model_Congratulations extends Zend_Db_Table_Abstract
    {
        protected $_name = 'congratulations';
        protected $_primary = 'id';
        protected $_db;
    
    public function __construct() {
            $this->_db = Zend_Registry::get('db');
        }
    
    public function add($data){
        $this->_db->insert($this->_name, $data);
    }
    public function listCongratulations($id) {
            $data = array(
                'id',
                'fullname',
                'content',
                'create_time',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->order('id DESC');
            if($id ==1){
                return $sql;
            }
            else{
                $result = $this->_db->fetchAll($sql);
                return $result;
            }
        }
     function nicetime($date)
        {
            if(empty($date)) {
                return "No date provided";
            }
            
            $periods         = array("giây", "phút", "giờ", "ngày", "tuần", "tháng", "năm", "thập kỉ");
            $lengths         = array("60","60","24","7","4.35","12","10");
            
            $now             = time();
            $unix_date       = strtotime($date);
            
               // check validity of date
            if(empty($unix_date)) {    
                return "Bad date";
            }
        
            // is it future date or past date
            if($now > $unix_date) {    
                $difference     = $now - $unix_date;
                $tense         = "trước";
                
            }
            
            for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
                $difference /= $lengths[$j];
            }
            
            $difference = round($difference);
            
            if($difference != 1) {
                $periods[$j].= "";
            }
            
            return "$difference $periods[$j] {$tense}";
        }
    }